// pages/home/home.js
import {
    request
} from '../../request/request'

Page({

    data: {
        // 身份信息(1-管理员；2-学生；3-老师)
        roleId:1,
        // 选择器index
        subjectIndex: 1,
        chapterIndex: 0,
        sectionIndex: 0,
        //临时数据，请求完后存储到multiArray[]中
        subject: [],
        chapter: [],
        section: [],
        // 当前学科章节id
        subjectId: 0,
        chapterId: 0,
        sectionId: 0,
        // 当前页数，从1开始
        pageNum: 1,
        // 后端返回的总页数
        totalPageNum: 0,
        // 当前是否是最后一页
        isBottom: false,
        // 触底事件防抖动
        Loading: false,
        // 获取的题目列表
        questionList: [],
        // 搜索内容
        searchTitle: "",
        // 处理后展示的数据
        showList: []
    },
    // onLoad先将数据初始化，触发initSubject函数
    onLoad(options) {
        console.log('onshow')
        this.setData({
            pageNum: 1,
            isBottom: false,
            subjectIndex: 1,
            chapterIndex: 0,
            sectionIndex: 0,
            showList: [],
            questionList: [],
            roleId:wx.getStorageSync('roleId')
        })
        this.initSubject()
    },
    // 初始化选择器中数据，获取初始学科章节，并在最后获取题目列表
    initSubject() {
        request({
            url: "/subject/listSubject",
            header: {
                "Authorization": wx.getStorageSync('token')
            },
            method: 'POST',

        }).then(res => {
            console.log("获取学科列表：：")
            this.setData({
                subject: [{
                    id: 0,
                    subjectName: "全部学科"
                }, ...res.data.data],

                subjectId: res.data.data[0].id
            })
            console.log(this.data.subjectId)
        }).then(res => {
            this.getChapterList([this.data.subjectId])
            setTimeout(() => {
                this.getSectionList([this.data.chapterId])
            }, 100);
            setTimeout(() => {
                this.questionRequest()
            }, 300);
        })
    },
    //获取学科
    getSubjectList() {
        request({
            url: "/subject/listSubject",
            header: {
                "Authorization": wx.getStorageSync('token')
            },
            method: 'POST',

        }).then(res => {
            console.log("获取学科列表：：")
            this.setData({
                subject: [{
                    id: 0,
                    subjectName: "全部学科"
                }, ...res.data.data],
                subjectId: res.data.data[0].id
            })
            // this.data.subject.unshift([{id:0,subjectName:"全部学科"}])
            console.log(this.data.subject)
        })
    },
    // 获取章列表
    getChapterList(subjectId) {
        console.log("subjectId:" + subjectId)
        request({
            url: "/chapter/listChapter",
            header: {
                "Authorization": wx.getStorageSync('token')
            },
            data: {
                "ids": subjectId
            },
            method: 'POST',

        }).then(res => {
            console.log('获取章列表：')
            console.log(res.data.data)
            console.log("this.data.chapterId:" + this.data.chapterId[0])
            this.setData({
                chapter: res.data.data,
                chapterId: res.data.data[0].id
            })
        })
    },
    //获取节
    getSectionList(SectionId) {
        // this.data.multiArray[2] = []
        request({
            url: "/section/listSection",
            header: {
                "Authorization": wx.getStorageSync('token')
            },
            data: {
                "ids": SectionId
            },
            method: 'POST',

        }).then(res => {
            console.log('获取节列表：')
            console.log(res.data.data)
            this.setData({
                section: res.data.data,
                sectionId: res.data.data[0].id
            })
        })
    },

    // 请求题目信息列表 
    questionRequest() {
        this.setData({
            pageNum: 1,
            showList: [],
            questionList: [],
            isBottom: false,
            Loading: false

        })
        request({
            url: "/question/pageQuestion?page=" + this.data.pageNum + "&pageSize=5&userId="+wx.getStorageSync('userId'),
            header: {
                "Authorization": wx.getStorageSync('token')
            },
            method: 'POST',
            data: {
                "subjectId": this.data.subjectId,
                "chapterId": this.data.chapterId,
                "sectionId": this.data.sectionId
            }
        }).then(res => {
            wx.hideLoading()
            console.log("题目查询：")
            console.log(res.data.data.records)
            this.setData({
                questionList: res.data.data.records,
                totalPageNum: res.data.data.pages
            })
            setTimeout(() => {
                this.renderLatex()
            }, 500);
        })
    },

    // 学科选择器
    subjectChange(e) {
        if (e.detail.value == 0) {
            this.setData({
                subjectIndex: e.detail.value,
                chapterIndex: -1,
                sectionIndex: -1,
                chapterId: 0,
                sectionId: 0,
                subjectId: 0
            })
            wx.showLoading({
                title: '加载中',
            })
            setTimeout(() => {
                this.questionRequest()
                wx.hideLoading()
            }, 500);
        } else {
            console.log(e)
            this.setData({
                subjectIndex: e.detail.value,
                subjectId: this.data.subject[e.detail.value].id,
                chapterIndex: 0,
                sectionIndex: 0
            })
            this.getChapterList([this.data.subjectId])
            wx.showLoading({
                title: '加载中',
            })
            setTimeout(() => {
                this.questionRequest()
                wx.hideLoading()
            }, 500);
        }

    },
    // 章选择器
    chapterChange(e) {
        this.setData({
            chapterIndex: e.detail.value,
            chapterId: this.data.chapter[e.detail.value].id,
            sectionIndex: 0
        })
        this.getSectionList([this.data.chapterId])
        wx.showLoading({
            title: '加载中',
        })
        setTimeout(() => {
            this.questionRequest()
            wx.hideLoading()
        }, 500);
    },
    // 节选择器
    sectionChange(e) {
        this.setData({
            sectionIndex: e.detail.value,
            sectionId: this.data.section[e.detail.value].id
        })
        wx.showLoading({
            title: '加载中',
        })
        setTimeout(() => {
            this.questionRequest()
            wx.hideLoading()
        }, 500);
    },

    //数据处理
    renderLatex: function () {
        this.setData({
            showList: []
        })
        console.log('renderLatex')
        this.data.questionList.forEach(item => {
            var str = item.title.split("<picture>")
            this.data.showList.push(str)
        })
        this.setData({
            showList: this.data.showList
        });
    },
    // 搜索框内容绑定
    inputSearchTitle(e) {
        console.log(e.detail.value)
        this.setData({
            searchTitle: e.detail.value
        })
    },
    // 点击搜索触发搜索函数，搜索符合条件的题目
    search() {
        console.log("search__")
        this.setData({
            pageNum: 1,
            isBottom: false,
            Loading: false
        })
        request({
            url: "/question/pageQuestionCollection?page=" + this.data.pageNum + "&pageSize=5",
            header: {
                "Authorization": wx.getStorageSync('token')
            },
            method: 'POST',
            data: {
                "subjectId": 0,
                "chapterId": 0,
                "sectionId": 0,
                "title": this.data.searchTitle
            }
        }).then(res => {
            console.log("search：：")
            console.log(res)
            this.setData({
                totalPageNum: res.data.data.pages,
                showList: [],
                questionList: res.data.data.records,
            })
            setTimeout(() => {
                this.renderLatex()
            }, 500);
        })
    },
    // 触底翻页
    scrollReachBottom(e) {
        console.log("触底")
        console.log(e)
        // 页数++
        this.data.pageNum++
        //当前页数大于总页数
        if (this.data.pageNum > this.data.totalPageNum) {
            this.setData({
                isBottom: true
            })
        }
        //已经加载完毕
        if (this.data.isBottom) {
            wx.showToast({
                title: '已加载完全部题目',
                icon: "none"
            })
            return
        }
        //没加载完全部题目
        else {
            console.log("还没加载完")
            if (this.data.Loading) {
                return
            }
            this.setData({
                Loading: true
            })
            wx.showLoading({
                title: '加载中',
            })
            // 发送下一页请求
            //如果searchTitle存在
            if (this.data.searchTitle) {
                request({
                    url: "/question/pageQuestionCollection?page=" + this.data.pageNum + "&pageSize=5",
                    header: {
                        "Authorization": wx.getStorageSync('token')
                    },
                    method: 'POST',
                    data: {
                        "subjectId": 0,
                        "chapterId": 0,
                        "sectionId": 0,
                        "title": this.data.searchTitle
                    }
                }).then(res => {
                    wx.hideLoading()
                    console.log("题目查询：")
                    console.log(res.data.data.records)

                    this.setData({
                        Loading: false,
                        questionList: [...this.data.questionList, ...res.data.data.records],
                    })
                    setTimeout(() => {
                        this.renderLatex()
                    }, 500);
                })
            } else {
                request({
                    url: "/question/pageQuestionCollection?page=" + this.data.pageNum + "&pageSize=5",
                    header: {
                        "Authorization": wx.getStorageSync('token')
                    },
                    method: 'POST',
                    data: {
                        "subjectId": this.data.subjectId,
                        "chapterId": this.data.chapterId,
                        "sectionId": this.data.sectionId
                    }
                }).then(res => {
                    wx.hideLoading()
                    console.log("题目查询：")
                    console.log(res.data.data.records)

                    this.setData({
                        Loading: false,
                        questionList: [...this.data.questionList, ...res.data.data.records],
                    })
                    setTimeout(() => {
                        this.renderLatex()
                    }, 500);
                })
            }

        }
    }

})